<?php
/**
 * Created by LíJimpéy(：
 * User: jimpey88
 * Date: 2024/3/26
 * Email: < jimpey@qq.com >
 * Project: company-admin-tp6
 */

namespace app\common\Services;
use think\exception\HttpResponseException;
use think\Response;
use think\response\Json;

trait JsonService
{
    protected function success($data, $msg = 'success'): \think\response\Json
    {
        return json([
            'code' => 200,
            'msg' => lang($msg),
            'data' => $data
        ]);
    }

    protected function error($msg, $code = 500, $status = 200, $data = []): \think\response\Json
    {
        return json([
            'code' => $code,
            'msg' => lang($msg),
            'data' => $data
        ], $status);
    }

    protected function successMsg($msg = 'success'): \think\response\Json
    {
        return json([
            'code' => 200,
            'msg' => lang($msg),
            'data' => []
        ]);
    }

    public static function throw(string $msg = 'fail', array $data = [], int $code = 400): Json
    {
        $msg = lang($msg);
        $data = compact('code', 'msg', 'data');
        $response = Response::create($data, 'json', 200);
        throw new HttpResponseException($response);
    }


}